#ifndef FGE_TESTSCREEN_HPP
#define FGE_TESTSCREEN_HPP

#include <ScreenManager.hpp>
#include <GameScreen.hpp>

#include <SpriteBatch.hpp>
#include <DeminaAnimation.hpp>
#include <AssetManager.hpp>
#include <Audio.hpp>
#include <RenderTexture.hpp>
#include <Font.hpp>
#include <Color.hpp>
#include <Camera.hpp>

namespace fge
{

class TestScreen : public GameScreen
{
public:
	void LoadContent()
	{
		frames = 0;
		seconds = 0;
		fps = 0;

		assetManager = new AssetManager();
		spriteBatch = new SpriteBatch();
		deminaPlayer = new DeminaAnimationPlayer();

		deminaPlayer->AddAnimation("walk", assetManager->LoadDeminaAnimation("animations/DeminaGuy/guy_walk.anim"));
		deminaPlayer->AddAnimation("stand", assetManager->LoadDeminaAnimation("animations/DeminaGuy/guy_default.anim"));
		deminaPlayer->StartAnimation("walk");

		song = assetManager->LoadSong("audio/Kevin MacLeod - Danse Macabre - Xylophone.ogg");
		coinEffect = assetManager->LoadSoundEffect("audio/coin.wav");
		explodeEffect = assetManager->LoadSoundEffect("audio/explode.wav");

		font = assetManager->LoadFont("fonts/test.binfont");
		camera = new Camera(800, 600);
		camera->SetPosition(Vector2(400, 300));
		angle = 0;
		zoom = 1.0f;
		direction = -1.0f;
	}

	void UnloadContent()
	{
		delete deminaPlayer;
		delete spriteBatch;
		delete assetManager;
	}

	void Update(float deltaSeconds)
	{
		angle += 3.14159f * deltaSeconds;
		camera->SetRotation(angle);

		zoom += deltaSeconds / 3 * direction;
		if(zoom > 1.0f || zoom < 0.25f)
			direction *= -1;

		camera->SetZoom(zoom);

		frames++;
		seconds += deltaSeconds;

		if(seconds >= 1)
		{
			seconds -= 1;
			fps = frames;
			frames = 0;
		}

		Game *game = GetScreenManager()->GetGame();
		if(game->IsKeyDown(Key_Escape))
			game->Exit();

		if(game->IsNewKeyDown(Key_Z))
			deminaPlayer->TransitionAnimation("stand", 1.0f);
		else if(game->IsNewKeyDown(Key_X))
			deminaPlayer->TransitionAnimation("walk", 1.0f);

		if(game->IsNewKeyDown(Key_A))
			game->PlaySoundEffect(coinEffect, false);
		if(game->IsNewKeyDown(Key_S))
			game->PlaySoundEffect(explodeEffect, false);

		if(game->IsNewKeyDown(Key_B))
			game->GetMusicManager()->Stop(2);

		if(game->IsNewKeyDown(Key_Space))
			game->GetMusicManager()->Play(song, 2, 2);

		if(game->IsKeyDown(Key_1))
			deminaPlayer->SetTime(0.25f);
		else if(game->IsKeyDown(Key_2))
			deminaPlayer->SetTime(0.50f);
		else if(game->IsKeyDown(Key_3))
			deminaPlayer->SetTime(0.75f);
		else
			deminaPlayer->Update(deltaSeconds);
	}

	void Draw()
	{
		GetScreenManager()->GetGame()->ClearScreen(1, 1, 1, 1);

		spriteBatch->Begin(BM_Alpha, camera->GetTransform());
		deminaPlayer->Draw(spriteBatch, Vector2(400, 300));
		spriteBatch->End();

		char fpsString[64];
		std::sprintf(fpsString, "%d", fps);

		spriteBatch->Begin(BM_Alpha, camera->GetTransform());
		spriteBatch->DrawString(font,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL\nMNOPQRSTUVWXYZ0123456789`~!@#$\n%^&*()_+-=[]\\{}|,./<>?;':\"", Vector2(0, 50), Color::Black);
		spriteBatch->DrawString(font, fpsString, Vector2(0, 600), Color::Black);
		spriteBatch->End();
	}

private:
	AssetManager *assetManager;
	SpriteBatch *spriteBatch;
	DeminaAnimationPlayer *deminaPlayer;
	Song *song;
	SoundEffect *coinEffect, *explodeEffect;
	Font *font;
	Camera *camera;

	int frames;
	float seconds;
	int fps;
	float angle;
	float zoom;
	float direction;
};

}

#endif//FGE_TESTSCREEN_HPP
